package com.ctsi.offlinesupport.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.offlinesupport.constant.SupportFlowConstant;
import com.ctsi.supportflow.app.entity.SupportInfo;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author: zhangjw
 * @Date: 2018/10/22 10:34
 * @Description: 自动派单委托实现
 *
 */
@Component("autoSendDelegate")
public class AutoSendDelegate implements JavaDelegate, Serializable {

    private static final Logger logger = LoggerFactory.getLogger(AutoSendDelegate.class);
    private static final long serialVersionUID = 2852985589578380036L;

    @Autowired
    private SupportUtilService userExpertEntityService;
    @Autowired
    private SupportInfoService supportInfoService;

    @Override
    public void execute(DelegateExecution execution) {

        int isAuto = (int) execution.getVariable(SupportFlowConstant.Variables.ISAUTO.name);
        int autoSendCount = (int) execution.getVariable(SupportFlowConstant.Variables.AUTOSENDCOUNT.name);
        logger.warn("send order with params isAuto: {},autoSendCount: {}",isAuto,autoSendCount);
        if (isAuto == 1 && autoSendCount > 0 ) {
            /**
             * 指定支撑经理走自动派单，和真正的自动派单相比，指定支撑经理直走一次
             */
            String supportorId = (String) execution.getVariable("supportorId");
            if (UtilValidate.isNotEmpty(supportorId)) {
                logger.warn("find support man {} so auto send the support man!");

                String[] vars = supportorId.split(",");
                execution.setVariable("supportUsers",supportorId);
                execution.setVariable("supportUserList", Arrays.asList(vars));
                /**
                 * 将自动派单次数设置为0，
                 */
                execution.setVariable(SupportFlowConstant.Variables.AUTOSENDCOUNT.name,0);
                /**
                 * 将派单方式改为2
                 */
                String bk = (String) execution.getVariable(SupportFlowConstant.Variables.BUSINESSKEY.name);
                SupportInfo info = new SupportInfo();
                info.setIsAuto(2);
                info.setId(bk);
                supportInfoService.updateSupportInfoByPKSelective(info);
                return;
            }


            List<String> sms = (ArrayList<String>) execution.getVariable(SupportFlowConstant.Variables.CANDIDATESUPPORTOR.name);
            if (UtilValidate.isEmpty(sms) && autoSendCount == 3) {
                String organId = (String) execution.getVariable(SupportFlowConstant.Variables.APPLYUSERORGANID.name);
                String supportType = (String) execution.getVariable(SupportFlowConstant.Variables.SUPPORTTYPE.name);
                List<String> src = userExpertEntityService.findSupportManager(organId,supportType);
//                /**
//                 * 指定支撑经理
//                 */
//                String supportorId = (String) execution.getVariable("supportorId");
//                if (UtilValidate.isNotEmpty(supportorId)) {
//                    logger.warn("find support man {} so auto send the support man first!");
//                    src.add(0,supportorId);
//                }
                sms = new ArrayList<>(src);
                Collections.shuffle(sms);
            }

            if (UtilValidate.isEmpty(sms)) {
                execution.setVariable(SupportFlowConstant.Variables.ISAUTO.name,0);
            } else {
                String userId = sms.remove(0);
                String[] vars = {userId};

                execution.setVariable("supportUsers",userId);
                execution.setVariable("supportUserList", Arrays.asList(vars));

                execution.setVariable(SupportFlowConstant.Variables.CANDIDATESUPPORTOR.name,sms);
                execution.setVariable(SupportFlowConstant.Variables.AUTOSENDCOUNT.name,sms.size() > autoSendCount-1 ? autoSendCount-1 : sms.size());
            }
        } else {
            /**
             * 不满足自动派单条件，改为手动派单
             */
//            String supportorId = (String) execution.getVariable("supportorId");
//
//            if (UtilValidate.isNotEmpty(supportorId)) {
//                String[] vars = {supportorId};
//                execution.setVariable("supportUsers",supportorId);
//                execution.setVariable("supportUserList", Arrays.asList(vars));
//            }
            execution.setVariable(SupportFlowConstant.Variables.AUTOSENDCOUNT.name,0);
            execution.setVariable(SupportFlowConstant.Variables.ISAUTO.name,0);
        }
    }

}
